package Worker;

import Module.StatusService;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Hashtable;

/*
 * Exposed service of job and worker status querying
 */
public class StatusInfo extends UnicastRemoteObject
            implements StatusService {
    private RegistryInfo registryInfo;
    private StatusService.WorkerStatus workerStatus;
    private Hashtable<Integer, JobStatus> jobsStatus;

    /*
     * Take in reference to the security service instance
     */
    public StatusInfo(RegistryInfo registryInfo) throws RemoteException {
        super();
        this.registryInfo = registryInfo;
        this.jobsStatus = new Hashtable<Integer, StatusService.JobStatus>();
    }

    /*
     * Set status of local worker
     */
    public void setWorkerStatus(WorkerStatus status){
        this.workerStatus = status;
    }

    /*
     * Allow master to see the status of this worker
     */
    public WorkerStatus getWorkerStatus(String authCode) 
    		throws RemoteException {
        if(!registryInfo.verifyAuthCode(authCode)) {
            return null;
        }

        return this.workerStatus;
    }

    /*
     * Set status of job on worker remotely (for restarting, cancelling etc)
     */
    public void setJobStatus(int jobID, JobStatus jobStatus, String authCode) 
    		throws RemoteException {
        if(!registryInfo.verifyAuthCode(authCode)) {
            return;
        }
        
        this.jobsStatus.put(jobID,jobStatus);
    }
    
    /*
     * Set status of job on worker
     */
    public void setJobStatus(int jobID, JobStatus jobStatus){       
        this.jobsStatus.put(jobID,jobStatus);
    }

    /*
     * Allow master to see the status of job
     */
    public JobStatus getJobStatus(int jobID, String authCode ) 
    	throws RemoteException{
        if(!registryInfo.verifyAuthCode(authCode)) {
            return null;
        }

        return this.jobsStatus.get(jobID);
    }

    /*
     * Allow master to get the status list of ALL jobs passed to the worker
     */
	public Hashtable<Integer, JobStatus> getJobsStatus(String authCode)
			throws RemoteException {
		 if(!registryInfo.verifyAuthCode(authCode)) {
	            return null;
	       }
		 return this.jobsStatus;

	}

	/*
	 * Allow master to remotely clear status of job
	 */
	public void clearStatus(String authCode) throws RemoteException {
		this.jobsStatus.clear();
	}
}
